package com.upbos.upm.dict;


import com.upbos.data.core.Pagination;
import com.upbos.data.core.SingleDao;
import com.upbos.data.mutildatasource.DS;
import com.upbos.data.persistence.Where;
import com.upbos.upm.entity.Dict;
import com.upbos.upm.entity.DictItem;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * <b>Application name：</b> DictDao.java <br>
 * <b>Application describing: 数据字典dao</b> <br>
 * <b>Copyright：</b> Copyright &copy; 2015-2017 myzh.com。<br>
 * <b>Company：</b> yideb.com <br>
 * <b>Date：</b> 2017年10月18日 16:05 <br>
 * <b>author：</b> <a href="mailto:wangjz@miyzh.com"> Jason </a>
 * <b>version：</b>V5.0.0
 */
@Repository
@DS("dataSource")
public class DictDao {
    @Resource
    private SingleDao singleDao;


    public Pagination listDict(Integer pageNo, Integer pageSize, Long categoryId, String name) {
        Dict dict = new Dict();
        dict.setCategoryId(categoryId);
        dict.setName(name);
        return singleDao.queryPaginationWithTotal(pageNo, pageSize, "DictDaoMapper.listDict", dict);
    }

    public int getDictCount(Dict dict) {
        return singleDao.queryOne("DictDaoMapper.getDictCount", dict);
    }


    public void insertDict(Dict dict) {
        singleDao.insert(dict);
    }

    public void updateDict(Dict dict) {
        singleDao.updateById(dict);
    }

    public List<DictItem> listDictItem(Long dictId) {
        return singleDao.queryList("DictDaoMapper.listDictItem", dictId);
    }


    public List<DictItem> listDictItemByDictKey(String dictKey) {
        return singleDao.queryList("DictDaoMapper.listDictItemByDictKey", dictKey);
    }

    public Dict getDict(Long dictId) {
        return singleDao.queryOne("DictDaoMapper.getDict", dictId);
    }

    public void insertDictItem(List<DictItem> dictItems) {
        singleDao.batchInsert(dictItems);
    }

    public void deleteDict(Dict dict) {
        singleDao.deleteById(dict);
    }


    public void deleteDictItem(Dict dict) {
        DictItem dictItem = new DictItem();
        dictItem.setDictId(dict.getId());

        singleDao.delete(dictItem, new Where("dict_id = #{dictId}"));
    }
}
